package com.liuyi.tools;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liuyi.tools.kits.RequestKit;
import com.liuyi.tools.web.resp.Response;
import com.liuyi.tools.web.resp.ResponseCall;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * <p><p>
 *
 * @ClassName WebUtil
 * @Aulthor Mr.fmy
 * @Version 1.0
 * @Date 2019/8/27 19:12
 **/
public class WebUtil {

    /**
     * 下载文件
     * (1) 下载后自动删除该文件
     *
     * @param file     需要下载的文件
     * @param fileName 文件名称 例如：sb.txt、该名称可随意填、主要为浏览器下载时的自动命名
     * @param response HttpServletResponse对象
     */
    public static void down(File file, String fileName, HttpServletResponse response) throws IOException {
        if (fileName != null) {
            //设置文件路径
            boolean isFile = false;
            if (file.exists()) {
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    //response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                    response.setContentType("application/octet-stream;charset=UTF-8");//也可以明确的设置一下UTF-8，测试中不设置也可以。
                    response.addHeader("Content-Length", "" + file.length());
                    response.setContentLength((int) file.length());
                    response.setHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes
                            ("GB2312"), "ISO-8859-1"));
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    isFile = true;
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        bis.close();
                    }
                    if (fis != null) {
                        fis.close();
                    }
                    if (isFile) {
                        file.delete();//下载完成后删除文件
                    }
                }
            } else {
                throw new RuntimeException("该文件不存在");
            }
        }
    }

    /**
     * 分页返回
     */
    public static <T> Response<T> page(Supplier<T> supplier) {
        Page<Map<String, Object>> pageInfo = PageHelper.startPage(RequestKit.getPageIndex(), RequestKit.getPageSize(), true);
        return ResponseCall.page(supplier.get(), pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize());
    }

    /**
     * 分页返回
     */
    public static <T> Response<T> page(Integer pageIndex, Integer pageSize, Supplier<T> supplier) {
        Page<Map<String, Object>> pageInfo = PageHelper.startPage(pageIndex, pageSize, true);
        return ResponseCall.page(supplier.get(), pageInfo.getTotal(), pageIndex, pageSize);
    }

    public static <T> Response<List<T>> page(IPage<T> page) {
        return ResponseCall.page(page.getRecords(), page.getTotal(), Convert.toInt(page.getCurrent()), Convert.toInt(page.getSize()));
    }

    public static <T> Response<List<T>> page(IPage<T> page, Consumer<List<T>> consumer) {
        List<T> datas = page.getRecords();
        consumer.accept(datas);
        return ResponseCall.page(datas, page.getTotal(), Convert.toInt(page.getCurrent()), Convert.toInt(page.getSize()));
    }
}
